package com.ncmem.up7.database;

import java.sql.*;

import com.ncmem.model.FileInf;
import com.ncmem.up7.sql.SqlExec;
import com.ncmem.up7.sql.SqlParam;


/*
 * 原型
 */
public class DBFolder {

    public DBFolder()
    {
    }

    public void remove(String id,String uid)
    {
        SqlExec se = new SqlExec();
        se.update("up7_folders",
                new SqlParam[]{
                        new SqlParam("f_deleted",true)
                },
                new SqlParam[]{
                new SqlParam("id",id),
                new SqlParam("uid",uid)
        });
    }

    static public int count(String id)
    {
        String sql = String.format("select count(*) from up7_files where f_pidRoot='%s'", id);

        DbHelper db = new DbHelper();
        int count = db.ExecuteScalar(sql);
        return count;
    }

    public void add(FileInf f)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("insert into up7_folders(");
        sb.append(" f_id");
        sb.append(",f_uid");
        sb.append(",f_nameLoc");
        sb.append(",f_pathLoc");
        sb.append(",f_pathSvr");
        sb.append(",f_pathRel");
        sb.append(",f_fileCount");
        sb.append(",f_pid");

        sb.append(") values (");

        sb.append(" ?");
        sb.append(",?");
        sb.append(",?");
        sb.append(",?");
        sb.append(",?");
        sb.append(",?");
        sb.append(",?");
        sb.append(",?");
        sb.append(")");

        DbHelper db = new DbHelper();
        PreparedStatement cmd = db.GetCommand(sb.toString());
        try {
            cmd.setString(1, f.id);
            cmd.setString(2, f.uid);
            cmd.setString(3, f.nameLoc);
            cmd.setString(4, f.pathLoc);
            cmd.setString(5, f.pathSvr);
            cmd.setString(6, f.pathRel);
            cmd.setInt(7, f.fileCount);
            cmd.setString(8, f.pid);
        } catch (SQLException e) {e.printStackTrace();}

        db.ExecuteNonQuery(cmd);
    }
}